package com.example.testapplication.binding.viewPager2;

import android.view.View;

import androidx.databinding.BindingAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import me.goldze.mvvmhabit.binding.command.BindingCommand;

public class ViewAdapter {
    @BindingAdapter({"onPageScrolledCommand","onPageScrollStateCommand"})
    public static void onregisterOnPageChangeCallbackCommand(ViewPager2 viewPager,
                                                             final BindingCommand<ScrollDataWrapper> onPageScrolledCommand,
                                                             final BindingCommand<Integer> onPageScrollStateCommand){
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                if(onPageScrolledCommand!=null){
                    onPageScrolledCommand.execute(new ScrollDataWrapper(position,positionOffset,positionOffsetPixels));
                }
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                if(onPageScrollStateCommand!=null){
                    onPageScrollStateCommand.execute(state);
                }
            }
        });
    }



    public static class ScrollDataWrapper{
        public int position;
        public float positionOffset;
        public int positionOffsetPixels;

        public ScrollDataWrapper(int position, float positionOffset, int positionOffsetPixels) {
            this.position = position;
            this.positionOffset = positionOffset;
            this.positionOffsetPixels = positionOffsetPixels;
        }
    }
}
